package com.demo.adaptdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class MainActivity extends AppCompatActivity {

    private List<Map<String,Object>> lst;
    private ListView lv;

    public void init(){
        lst = new ArrayList<Map<String,Object>>();
        Map<String,Object> mp1 = new HashMap<String, Object>();
        Map<String,Object> mp2 = new HashMap<String, Object>();
        Map<String,Object> mp3 = new HashMap<String, Object>();

        mp1.put("image",R.drawable.a);
        mp1.put("title","dsxjkds1");
        mp1.put("cxt","djamxje");

        mp2.put("image",R.drawable.b);
        mp2.put("title","dsxjkds2");
        mp2.put("cxt","djamxje");

        mp3.put("image",R.drawable.c);
        mp3.put("title","dsxjkds3");
        mp3.put("cxt","djamxje");

        lst.add(mp1);lst.add(mp2);lst.add(mp3);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        lv = (ListView)findViewById(R.id.lv);
        ListAdapter adapter = new SimpleAdapter(this,lst,R.layout.news_layout,new String[]{"image","title","cxt"},new int[]{R.id.imv,R.id.tv1,R.id.tv2});

        lv.setAdapter(adapter);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.i("gtq",lst.get(position).get("title").toString());
            }
        });
    }
}
